package leetcode._349;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

public class Solution {

	
	public int[] intersection(int[] nums1, int[] nums2) {
		TreeSet<Integer> set = new TreeSet<>();
		for (int n : nums1) {
			set.add(n);
		}
		
		List<Integer> list = new ArrayList<>();
		for (int n : nums2) {
			if (set.contains(n)) {
				list.add(n);
				set.remove(n);
			}
		}
		int[] res = new int[list.size()];
		for (int i = 0; i < list.size(); i++) {
			res[i] = list.get(i);
		}
		return res;
    }
}
